package com.hln.jingwushi.ui

import android.os.Bundle
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.hln.jingwushi.databinding.ActivityLoginBinding
import com.gdsz.libbasic.view.ui.BaseActivity


class LoginActivity : BaseActivity<ActivityLoginBinding>() {

    lateinit var url : String
    lateinit var func : String
    lateinit var webView : WebView
    override fun ActivityLoginBinding.initBinding(savedInstanceState: Bundle?) {
        url = intent.getStringExtra("url").toString()
        func = intent.getStringExtra("func").toString()
        initWebView()
    }


    fun initWebView () {
        webView = binding.webview
        binding.webview.settings.javaScriptEnabled = true
        binding.webview.clearCache(true)
        binding.webview.clearHistory();
        configureWebSettings(binding.webview.settings)
        binding.webview.setWebViewClient(HWebViewClient())
        binding.webview.loadUrl(url)
    }


    fun configureWebSettings(webSettings: WebSettings) {
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.javaScriptEnabled = true
        //设置自适应屏幕，两者合用
        webSettings.useWideViewPort = true //将图片调整到适合webview的大小
        webSettings.loadWithOverviewMode = true // 缩放至屏幕的大小

        //允许webview对文件的操作
        webSettings.allowUniversalAccessFromFileURLs = true
        webSettings.allowFileAccess = true
        webSettings.allowFileAccessFromFileURLs = true

        //缩放操作
        webSettings.setSupportZoom(true) //支持缩放，默认为true。是下面那个的前提。
        webSettings.builtInZoomControls = true //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.displayZoomControls = false //隐藏原生的缩放控件
        // 支持多窗口
        webSettings.setSupportMultipleWindows(true)
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
        webSettings.domStorageEnabled = true // 开启 DOM storage API 功能
        webSettings.databaseEnabled = true //开启 database storage API 功能
        webSettings.cacheMode = WebSettings.LOAD_DEFAULT;
//        webSettings.setAppCacheEnabled(true) //开启 Application Caches 功能
//        webSettings.setAppCachePath(getExternalCacheDir()!!.getAbsolutePath())
        webSettings.allowFileAccess = true //设置可以访问文件
        webSettings.javaScriptCanOpenWindowsAutomatically = true //支持通过JS打开新窗口
        webSettings.loadsImagesAutomatically = true //支持自动加载图片
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
    }

    private class HWebViewClient : WebViewClient() {
//        override fun shouldOverriderlLoading(view: WebView, request: WebResourceRequest): Boolean {
//            val url = request.url.toSUtring()
//            //Log.d("WebClient", "url:" + url);
//            view.loadUrl(url)
//            return true
//        }
    }
}